package com.it.system.course_service.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.otter.canal.protocol.CanalEntry;
import com.it.system.course_service.config.CourseMQConfig;
import com.it.system.educommonapi.entity.Course;
import com.xpand.starter.canal.annotation.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;

/**
 * Canal事件监听器
 */
@Slf4j
@CanalEventListener
public class CanalListener {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 监听课程表的更新操作
     */
    @UpdateListenPoint(schema = "edu_course", table = "course")
    public void onCourseUpdate(CanalEntry.RowData rowData) {
        log.info("课程表更新：{}", rowData);
        // 构建课程对象
        Course course = new Course();
        rowData.getAfterColumnsList().forEach(column -> {
            if ("id".equals(column.getName())) {
                course.setId(Long.parseLong(column.getValue()));
            } else if ("course_name".equals(column.getName())) {
                course.setCourseName(column.getValue());
            } else if ("brief".equals(column.getName())) {
                course.setBrief(column.getValue());
            } else if ("price".equals(column.getName())) {
                course.setPrice(Double.parseDouble(column.getValue()));
            }
            // 添加其他需要的字段...
        });
        
        // 发送消息到RabbitMQ
        String jsonCourse = JSON.toJSONString(course);
        rabbitTemplate.convertAndSend(CourseMQConfig.EXCHANGE, CourseMQConfig.KEY_UPDATE, jsonCourse);
        log.info("发送课程更新消息：{}", jsonCourse);
    }

    /**
     * 监听课程表的删除操作
     */
    @DeleteListenPoint(schema = "edu_course", table = "course")
    public void onCourseDelete(CanalEntry.RowData rowData) {
        log.info("课程表删除：{}", rowData);
        String courseId = null;
        for (CanalEntry.Column column : rowData.getBeforeColumnsList()) {
            if ("id".equals(column.getName())) {
                courseId = column.getValue();
                break;
            }
        }
        if (courseId != null) {
            rabbitTemplate.convertAndSend(CourseMQConfig.EXCHANGE, CourseMQConfig.KEY_DELETE, courseId);
            log.info("发送课程删除消息，课程ID：{}", courseId);
        }
    }

    /**
     * 监听课程表的插入操作
     */
    @InsertListenPoint(schema = "edu_course", table = "course")
    public void onCourseInsert(CanalEntry.RowData rowData) {
        log.info("课程表插入：{}", rowData);
        // 构建课程对象
        Course course = new Course();
        rowData.getAfterColumnsList().forEach(column -> {
            if ("id".equals(column.getName())) {
                course.setId(Long.parseLong(column.getValue()));
            } else if ("course_name".equals(column.getName())) {
                course.setCourseName(column.getValue());
            } else if ("brief".equals(column.getName())) {
                course.setBrief(column.getValue());
            } else if ("price".equals(column.getName())) {
                course.setPrice(Double.parseDouble(column.getValue()));
            }
            // 添加其他需要的字段...
        });
        
        // 发送消息到RabbitMQ
        String jsonCourse = JSON.toJSONString(course);
        rabbitTemplate.convertAndSend(CourseMQConfig.EXCHANGE, CourseMQConfig.KEY_UPDATE, jsonCourse);
        log.info("发送课程新增消息：{}", jsonCourse);
    }
}